package restfuljobs

import org.grails.jaxrs.provider.DomainObjectNotFoundException

class JobListingResourceService {
    
    def create(JobListing dto) {
        dto.save()
    }

    def read(def id) {
        def obj = JobListing.get(id)
        if (!obj) {
            throw new DomainObjectNotFoundException(JobListing.class, id)
        }
        obj
    }
    
    def readAll() {
        JobListing.findAll()
    }
    
    def search(String jobTitle, String employer, String jobCategory, String location) {
        def hql = "from JobListing as j where j.jobTitle like '%" + jobTitle + "%'"
        hql += " and j.employer like '%" + employer + "%'"     
        hql += " and j.jobCategory like '%" + jobCategory + "%'"
        hql += " and j.location like '%" + location + "%'"
        hql += " and j.status = '" + JobListing.Status.OPEN + "'"
           
        def results = JobListing.findAll(hql)        
        return results
    }    
    
    def update(JobListing dto) {
        def obj = JobListing.get(dto.id)
        if (!obj) {
            throw new DomainObjectNotFoundException(JobListing.class, dto.id)
        }
        obj.properties = dto.properties 
        obj
    }
    
    void delete(def id) {
        def obj = JobListing.get(id)
        if (obj) { 
            obj.delete()
        }
    }
    
}

